package com.haohao.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.haohao.common.annotation.Anonymous;
import com.haohao.system.domain.SysAppliances;
import com.haohao.system.service.ISysAppliancesService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.haohao.common.annotation.Log;
import com.haohao.common.core.controller.BaseController;
import com.haohao.common.core.domain.AjaxResult;
import com.haohao.common.enums.BusinessType;
import com.haohao.system.domain.SysInfo;
import com.haohao.system.service.ISysInfoService;
import com.haohao.common.utils.poi.ExcelUtil;
import com.haohao.common.core.page.TableDataInfo;

/**
 * 库存记录Controller
 *
 * @author haohao
 * @date 2024-04-10
 */
@RestController
@RequestMapping("/system/info")
public class SysInfoController extends BaseController
{
    @Autowired
    private ISysInfoService sysInfoService;
    @Autowired
    private ISysAppliancesService sysAppliancesService;
    /**
     * 查询库存记录列表
     */
    @Anonymous

    @PreAuthorize("@ss.hasPermi('system:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysInfo sysInfo)
    {
        startPage();
        List<SysInfo> list = sysInfoService.selectSysInfoList(sysInfo);
        return getDataTable(list);
    }



    /**
     * 获取库存记录详细信息
     */
    @Anonymous

    @PreAuthorize("@ss.hasPermi('system:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(sysInfoService.selectSysInfoById(id));
    }

    /**
     * 新增库存记录
     */
    @Anonymous

    @PreAuthorize("@ss.hasPermi('system:info:add')")
    @Log(title = "库存记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysInfo sysInfo)
    {
        SysAppliances appliances = sysAppliancesService.selectSysAppliancesById(sysInfo.getTid());
        if (sysInfo.getType().equals("入库")){
            appliances.setCount(appliances.getCount()+sysInfo.getCount());
            sysAppliancesService.updateSysAppliances(appliances);
            sysInfo.setName(appliances.getName());
            return toAjax(sysInfoService.insertSysInfo(sysInfo));
        }
        else {
            if (appliances.getCount()>=sysInfo.getCount()){
                appliances.setCount(appliances.getCount()-sysInfo.getCount());
                sysAppliancesService.updateSysAppliances(appliances);
                sysInfo.setName(appliances.getName());
                return toAjax(sysInfoService.insertSysInfo(sysInfo));
            }
            else {
                return error("库存不足");

            }

        }

    }

    /**
     * 修改库存记录
     */
    @Anonymous

    @PreAuthorize("@ss.hasPermi('system:info:edit')")
    @Log(title = "库存记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysInfo sysInfo)
    {
        return toAjax(sysInfoService.updateSysInfo(sysInfo));
    }

    /**
     * 删除库存记录
     */
    @Anonymous

    @PreAuthorize("@ss.hasPermi('system:info:remove')")
    @Log(title = "库存记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysInfoService.deleteSysInfoByIds(ids));
    }
}
